package top.infopub.mgr.bpa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.bpa.PrimarySureDto;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.PackageSureAttDao;
import top.infopub.mgr.bpa.dao.PackageSureDao;
import top.infopub.mgr.bpa.dao.PackageSureDetailDao;
import top.infopub.mgr.bpa.domain.PackageSureDomain;
import top.infopub.mgr.bpa.service.PackageSureService;
import top.infopub.mgr.bpa.vo.PackageSureVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.util.ReflectBeanUtils;
import top.infopub.model.bpa.PackageSure;
import top.infopub.model.bpa.PackageSureAtt;
import top.infopub.model.bpa.PackageSureDetail;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by wurz on 2021/6/11.
 */
@Service
public class PackageSureServiceImpl extends GenericServiceImpl<PackageSure, String> implements
        PackageSureService {

    @Resource
    private PackageSureDao packageSureDao;

    @Autowired
    private WsFlowProcessService wsFlowProcess;

    @Resource
    private PackageSureDetailDao packageSureDetailDao;

    @Resource
    private PackageSureAttDao packageSureAttDao;


    @Override
    public GenericDao<PackageSure, String> getDao() {
        return packageSureDao;
    }

    @Override
    public Page<PackageSureDomain> selectPageByDomainWhere(Page<PackageSureDomain> page,
                                                          PackageSureVo sureVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<PackageSureDomain>)packageSureDao.selectByDomainWhere(sureVo);
    }

    @Override
    public Page<PackageSureDomain> selectMyCheckByDomainWhere(Page<PackageSureDomain> page,
                                                             PackageSureVo sureVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<PackageSureDomain>)packageSureDao.selectMyCheckByDomainWhere(sureVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(PackageSureVo sure, List<PackageSureDetail> sureDetails,
                     List<PackageSureAtt> sureAtts) {
        //插入记录
        packageSureDao.insertById(sure);

        //批量插入子项
        if(CollectionUtils.isNotEmpty(sureDetails)){
            packageSureDetailDao.insertByBatch(sureDetails);
        }

        //批量插入附件
        if(CollectionUtils.isNotEmpty(sureAtts)){
            packageSureAttDao.insertByBatch(sureAtts);
        }

        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.BPA_DEVICE_SURE;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, sure.getId(), sure.getProcessId(), null);

    }

    @Override
    public PackageSureVo queryById(String id) {
        return packageSureDao.queryById(id);
    }

    @Override
    public String insertById(PrimarySureDto primarySureDto, String creator) {
        PackageSureVo sureVo = ReflectBeanUtils.do2bo(primarySureDto, PackageSureVo.class);
        Date date = new Date();
        sureVo.setAddBy(creator);
        sureVo.setAddTime(date);
        sureVo.setUptBy(creator);
        sureVo.setUptTime(date);
        packageSureDao.insertSelective(sureVo);
        return sureVo.getId();
    }

    @Override
    public void updateByIdSelective(PrimarySureDto primarySureDto, String modifier) {
        PackageSureVo sureVo = ReflectBeanUtils.do2bo(primarySureDto, PackageSureVo.class);
        sureVo.setId(primarySureDto.getId());
        Date date = new Date();
        sureVo.setUptBy(modifier);
        sureVo.setUptTime(date);
        packageSureDao.updateByIdSelective(sureVo);
    }
}